package com.superhelp.ebill.api;

import android.content.Context;
import android.widget.Toast;

import com.superhelp.ebill.R;
import com.superhelp.ebill.api.error.AuthFailureError;
import com.superhelp.ebill.api.error.NetworkError;
import com.superhelp.ebill.api.error.NoConnectionError;
import com.superhelp.ebill.api.error.ParseError;
import com.superhelp.ebill.api.error.ServerError;
import com.superhelp.ebill.api.error.TimeoutError;
import com.superhelp.ebill.api.error.VolleyError;
import com.superhelp.ebill.util.FFToast;
import com.superhelp.ebill.util.StringUtils;

public class ResultCode {
	public static final int SUCCESS = 0;// 成功
	public static final int ERROR = -1;// 失败
	public static final int SIGNERROR = 1;// 签名错误
	public static final int SERVERNETERROR = 2;// 服务器网络错误
	public static final int STATIFYERROR = 3;// 安全错误
	public static final int throwableERROR = 4;// 内部错误
	public static final int DATAERROR = 5;// 数据错误
	public static final int PARAMERROR = 6;// 参数错误
	public static final int WORKERROR = 7;// 业务信息错误

	/**
	 * http错误码
	 * 
	 * @author 陈坤彬
	 *
	 */
	public class HttpStatusCode {
		public static final int REQUESTERROR = 404;
		public static final int SERVERERROR = 500;

	}

	/**
	 * 打印错误信息
	 * 
	 * @param mContext
	 * @param throwable
	 */
	public static void toastVolleyError(Context mContext,
			Throwable throwable) {

		try {
			if (throwable instanceof AuthFailureError) {
				// 如果在做一个HTTP的身份验证，可能会发生这个错误
				FFToast.makeText(mContext, StringUtils.getString(R.string.resultcodecontent1), Toast.LENGTH_LONG).show();
			}else if (throwable instanceof NoConnectionError) {
				// 和NetworkError类似，这个是客户端没有网络连接
				FFToast.makeText(mContext, StringUtils.getString(R.string.resultcodecontent2), Toast.LENGTH_LONG).show();
			} else if (throwable instanceof NetworkError) {
				// Socket关闭，服务器宕机，DNS错误都会产生这个错误
				FFToast.makeText(mContext, StringUtils.getString(R.string.resultcodecontent6), Toast.LENGTH_LONG).show();
			}  else if (throwable instanceof ParseError) {
				// 在使用JsonObjectRequest或JsonArrayRequest时，如果接收到的JSON是畸形，会产生异常
				FFToast.makeText(mContext, StringUtils.getString(R.string.resultcodecontent3), Toast.LENGTH_LONG).show();
			} else if (throwable instanceof TimeoutError) {
				// Socket超时，服务器太忙或网络延迟会产生这个异常
				FFToast.makeText(mContext, StringUtils.getString(R.string.resultcodecontent4), Toast.LENGTH_LONG).show();
			} else if (throwable instanceof ServerError) {
				// 服务器的响应的一个错误，最有可能的4xx或5xx
				FFToast.makeText(mContext, StringUtils.getString(R.string.resultcodecontent6), Toast.LENGTH_LONG)
						.show();
			} else {
				FFToast.makeText(mContext, throwable.getMessage(), Toast.LENGTH_LONG)
						.show();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
